<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@include file="/taglibs.jsp"%>
<!doctype html>
<html>
<head>
    <meta charset="UTF-8">  
    <meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <link href="${ctx}/resources/semantic/dist/semantic.min.css" rel="stylesheet" type="text/css" />
    <link href="${ctx}/resources/apps.css" rel="stylesheet" type="text/css" />
	<script src="${ctx}/resources/semanticui/jquery-2.1.4.min.js"></script>
	<script src="${ctx}/resources/semantic/dist/semantic.min.js" type="text/javascript"></script>
    <title>发布职位</title>
    <style>
        input{width:100px;height:30px;line-height:30px;}


    </style>
</head>
<body>
<div class="ui segment" id="n-title" style="height: 100px">
    <div id="firstTitle">职位基本信息</div>
    <div id="secondTitle" style="display: none">期望候选人</div>
</div>
<div class="ui segments" id="n-body">
    <div class="ui segment" id="n-page">
        <div class="ui segment" id="n-content">
            <form class="ui form">
            	 <!-- add by 20191120 adela 临时补职位归一 又第二次并更11/28 李尚良提出由原职位名称并更内部职位名称/与职位类别进行调换-->
                <div id="positionInfo">
                    <div class="field">
                        <label><span style="color: red">*</span>职位名称</label>
                        <select class="ui search dropdown" name="roleid" id="roleid">
                            <option value="">请选择职位</option>
                            <c:forEach items="${roles}" var="role">
                                <c:choose>
                                    <c:when test="${position.roleid == role.id }">
                                        <option value="${role.id}" selected="selected">${role.name}</option>
                                    </c:when>
                                    <c:otherwise>
                                        <option value="${role.id}">${role.name}</option>
                                    </c:otherwise>
                                </c:choose>
                            </c:forEach>
                        </select>
                    </div>
                    <div class="field">
                        <label><span style="color: red">*</span>对外显示职位名称</label>
                        <input type="text" name="jobtitle" id="jobtitle" value="${position.name}" placeholder="请输入对外显示职位名称">
                        <%-- <div class="ui search" id="jobtitle" name="jobtitle">
                            <div class="ui input">
                                <input class="prompt" type="text" placeholder="请输入职位名称" value="${position.name}">
                            </div>
                            <div class="results"></div>
                        </div> --%>
                    </div>

                    <div class="field">
                        <label><span style="color: red">*</span>所在城市</label>
                        <select name="cityId" id="cityId"  class="ui search dropdown">
                            <option value="">请输入所在城市名称</option>
                            <c:forEach items="${cities}" var="city">
                                <c:choose>
                                    <c:when test="${position.cityId == city.id }">
                                        <option value="${city.id}" selected="selected">${city.name}</option>
                                    </c:when>
                                    <c:otherwise>
                                        <option value="${city.id}">${city.name}</option>
                                    </c:otherwise>
                                </c:choose>
                            </c:forEach>
                        </select>
                    </div>
                    <div class="field">
                        <label><span style="color: red">*</span>负责区域</label>
                        <div class="ui input">
                            <input type="text" name="location" id="location" value="${position.location}" placeholder="请输入区域">
                        </div>
                    </div>
                    <div class="field">
                        <label><span style="color: red">*</span>下属人数</label>
                        <div class="ui input">
                            <input type="text" name="employeeNum" id="employeeNum" value="${position.employeeNum}" placeholder="请输入下属人数">
                        </div>
                    </div>
                    <div class="field">
                        <label>薪资中位值（单位：W）</label>
                        <div class="ui input">
                            <input type="text" name="annualPayment" id="annualPayment" value="${position.annualPayment}" placeholder="请输入年薪，单位为：W">
                        </div>
                    </div>
                    <div class="field">
                        <label><span style="color: red">*</span>关键要求</label>
                        <div class="ui input">
                            <textarea name="majorDuty" id="majorDuty">${position.majorDuty}</textarea>
                        </div>
                    </div>
                    <div class="field">
                        <label><span style="color: red">*</span>主要职责</label>
                        <div class="ui input">
                            <textarea name="description" id="description" >${position.description}</textarea>
                        </div>
                    </div>
                    <div class="field">
                        <label><span style="color: red">*</span>推荐奖金</label>
                        <div class="ui input">
                            <input type="text" name="reward" id="reward" value="${position.reward}" placeholder="请输入推荐奖金,单位为：元">
                        </div>
                    </div>

                    <div class="field">
                        <label><span style="color: red">*</span>请选择职位特点标签（最多选2项）</label>
                        <select class="ui search selection dropdown1"
                                name="keywords" id="keywords" multiple="" >
                            <option value="">请选择职位特点标签</option>
                            <c:forEach items="${tags}" var="tag">

                                <option value="${tag.value}">${tag.label}</option>

                            </c:forEach>
                        </select>

                    </div>



                </div>
                <div id="candidateInfo" style="display:none">
                <div class="field">
                    <label><span style="color: red">*</span>期望候选人目前职位</label>
                    <select name="expectJobId" id ="expectJobId" class="ui search dropdown">
                        <option value="" >请输入期望候选人目前职位</option>
                        <c:forEach items="${roles}" var="role">
                            <c:choose>
                                <c:when test="${position.expectJobId == role.id }">
                                    <option value="${role.id}" selected="selected">${role.name}</option>
                                </c:when>
                                <c:otherwise>
                                    <option value="${role.id}">${role.name}</option>
                                </c:otherwise>
                            </c:choose>
                        </c:forEach>
                    </select>
                </div>

                <div class="field">
                    <label><span style="color: red">*</span>期望候选人所在细分领域（最多选3项）</label>
                    <select class="ui search selection dropdown"
                            name="majordomain" id="majordomain" multiple="" >
                        <option value="">请输入期望候选人细分领域</option>
                        <c:forEach items="${domains}" var="dm">

                            <option value="${dm.id}">${dm.name}</option>

                        </c:forEach>
                    </select>

                </div>
                <div class="field">
                     <label><span style="color: red">*</span> 期望候选人所在公司来源1</label>
                    <select class="ui search dropdown" name="expectcompanyid" id="expectcompanyid">
                        <option value="">请输入期望来源公司名</option>
                        <c:forEach items="${companies}" var="company">
                            <c:choose>
                                <c:when test="${position.expectcompanyid == company.id }">
                                    <option value="${company.id}" selected="selected">${company.name}</option>
                                </c:when>
                                <c:otherwise>
                                    <option value="${company.id}">${company.name}</option>
                                </c:otherwise>
                            </c:choose>
                        </c:forEach>
                    </select>
                </div>
                <div class="field">
                    <label>期望候选人所在公司来源2</label>
                    <select class="ui search dropdown" name="expectcompanyid2" id="expectcompanyid2">
                        <option value="0">请输入期望来源公司名</option>
                        <c:forEach items="${companies}" var="company">
                            <c:choose>
                                <c:when test="${position.expectcompanyid2 == company.id }">
                                    <option value="${company.id}" selected="selected">${company.name}</option>
                                </c:when>
                                <c:otherwise>
                                    <option value="${company.id}">${company.name}</option>
                                </c:otherwise>
                            </c:choose>
                        </c:forEach>
                    </select>
                </div>
                <div class="field">
                    <label>期望候选人所在公司来源3</label>
                    <select class="ui search dropdown" name="expectcompanyid3" id="expectcompanyid3">
                        <option value="0">请输入期望来源公司名</option>
                        <c:forEach items="${companies}" var="company">
                            <c:choose>
                                <c:when test="${position.expectcompanyid3 == company.id }">
                                    <option value="${company.id}" selected="selected">${company.name}</option>
                                </c:when>
                                <c:otherwise>
                                    <option value="${company.id}">${company.name}</option>
                                </c:otherwise>
                            </c:choose>
                        </c:forEach>
                    </select>
                </div>
                <div class="field">
                    <label><span style="color: red">*</span>职位有效期</label>
                    <div class="ui input">
                        <input type="date" name="expiredDateStr" id= "expiredDateStr" value="${position.expiredDateStr}">
                    </div>
                </div>
                <div class="field">
                    <label>备注</label>
                    <div class="ui input">
                        <input type="text" name="comment" id="comment" value="${position.comment}" placeholder="请输入备注">
                    </div>
                </div>
                <div class="field">
                    <label><span style="color: red">*</span>选择要发布的平台（可多选）</label>
                    <div class="ui center aligned segment" id="choice-buttons">
                        <div class="two ui buttons">
                            <c:choose>
                                <c:when test="${position.platformID == 1 }">
                                    <div class="ui button dog" id="hunter">猎头平台</div>
                                    <div class="ui button active dog" id="recom">候选人平台</div>
                                </c:when>
                                <c:when test="${position.platformID == 2 }">
                                    <div class="ui button active dog" id="hunter">猎头平台</div>
                                    <div class="ui button dog" id="recom">候选人平台</div>
                                </c:when>
                                <c:when test="${position.platformID == 3 }">
                                    <div class="ui button active dog" id="hunter">猎头平台</div>
                                    <div class="ui button active dog" id="recom">候选人平台</div>
                                </c:when>
                                <c:otherwise>
                                    <div class="ui button dog" id="hunter">猎头平台</div>
                                    <div class="ui button dog" id="recom">候选人平台</div>
                                </c:otherwise>
                            </c:choose>
                        </div>
                    </div>
                </div>

                <input type="hidden" name="positionid" id="positionid" value="${position.id}"/>
                <input type="hidden" name="domainid" id="domainid" value="${position.domainID}"/>

            </div>


                <div class="ui segment" id="btn-next2">
                    <div class="fluid ui button" id="next" >下一步</div>
                </div>
                <div class="ui segment" id="cont-next" style="display: none">
                    <div class="fluid ui button" id="back" style="display: inline;width: 48%;float: left">上一步</div>
                    <div class="fluid ui button" id="publish" style="display: inline;width: 48%;float: right">发布职位</div>
                    <br><br>
                </div>

            </form>
        </div>

    </div>
</div>

<script src="${ctx}/resources/jweixin-1.0.0.js"></script>

<script type="text/javascript">

var dataarray=[];
var keywordList=[];
var keywords = "${position.keywords}"
keywordList = keywords.split(",")
dataarray[0] = "${position.majorDomainId1}";
dataarray[1] = "${position.majorDomainId2}";
dataarray[2] = "${position.majorDomainId3}";
var expectCompany = '${position.expectcompanyid}';
var expectCompany2 = '${position.expectcompanyid2}';
var expectCompany3 = '${position.expectcompanyid3}';

wx.config({
    debug: false, 
    appId: "${config.appid}",  
    timestamp: ${config.currenttimestamp},  
    nonceStr: "${config.nonceStr}", 
    signature: "${config.signatureStr}", 
    jsApiList: ['checkJsApi',
        'onMenuShareTimeline',
        'onMenuShareAppMessage',
        'onMenuShareQQ',
        'onMenuShareWeibo',
        'hideOptionMenu']  
});


    function changeDomain() {
        debugger;

    }

    var handler = { 
        activate: function() { 
            //$(this).addClass('active').siblings().removeClass('active'); 
            if($(this).attr('id') == 'hunter') {
                if($(this).hasClass('active')) {
                    $(this).removeClass('active'); 
                } else {
                    $(this).addClass('active');
                }
            }
            if($(this).attr('id') == 'recom') {
                if($(this).hasClass('active')) {
                    $(this).removeClass('active'); 
                } else {
                    $(this).addClass('active');
                }
            }
            if($(this).attr('id') == 'next') {
                var positionid  = $("#positionid").val();
                var roleid      = $("#roleid").val();
                var jobname     = $('#jobtitle').val()
                var cityid      = $("#cityId").val();
                var location    = $("#location").val();
                var employeeNum = $("#employeeNum").val();
                var description = $('#description').val();
                var majorDuty   = $("#majorDuty").val();
                var reward      = $("#reward").val();
                var keywords = $('#keywords :selected');


                if(!roleid) {
                    alert("请必须选择职位");
                    return false;
                }
                if(!jobname || jobname == "") {
                    alert("请填写对外显示的职位名称");
                    return false;
                }

                if(!cityid) {
                    alert("请选择城市");
                    return false;
                }

                if(!location || location=="") {
                    alert("请填写负责区域");
                    return false;
                }
                if(location.length > 40) {
                    alert("负责区域名字太长");
                    return false;
                }
                if(!employeeNum || employeeNum =="") {
                    alert("请填写下属人数");
                    return false;
                }
                if(!/^[0-9]+$/.test(employeeNum) || employeeNum < 0 || employeeNum > 50000) {
                    alert("您填写的下属人数不合法，请填写大于0的正整数");
                    return false;
                }
                if(!majorDuty || majorDuty =="") {
                    alert("请填写职位关键要求");
                    return false;
                }


                if(!description || description =="") {
                    alert("请填写主要职责");
                    return false;
                }

                /* if(!reward || reward =="") {
                    alert("您必须输入推荐奖金");
                    return false;
                } */
                if(!/^[0-9]+$/.test(reward) || reward < 0 || reward > 100000) {
                    alert("推荐奖金区间在0~100000元");
                    return false;
                }

                $("#candidateInfo").show();
                $("#cont-next").show();
                $("#secondTitle").show();
                $("#firstTitle").hide();
                $("#positionInfo").hide();
                $("#btn-next2").hide();
            }
            if($(this).attr('id') == 'back') {
                $("#positionInfo").show();
                $("#btn-next2").show();
                $("#firstTitle").show();
                $("#secondTitle").hide();
                $("#candidateInfo").hide();
                $("#cont-next").hide();
            }

            if($(this).attr('id') == 'publish') {
                var positionid  = $("#positionid").val();
                var roleid      = $("#roleid").val();
                var cityid      = $("#cityId").val();
                var location    = $("#location").val();
                var employeeNum = $("#employeeNum").val()
                var majorDuty   = $("#majorDuty").val();
                var reward      = $("#reward").val();
                var description = $('#description').val();
                var keywords = $('#keywords :selected');


                var expectJobId     = $("#expectJobId").val();
                var expectJobName = '';
                if (expectJobId != 0) {
                     expectJobName     = $("#expectJobId option:checked").text();

                }
                console.log("expectJobId:"+expectJobId);
                console.log("expectJobName:"+expectJobName);
                var expiredDateStr  = $("#expiredDateStr").val();
                var comment         = $("#comment").val();
                var domainid         = $("#domainid").val();
                var majordomain  = $("#majordomain").val();
                var annualPayment = $("#annualPayment").val();
                var platformid  = 2;

               // var jobname = $('#jobtitle').search('get value');
                var jobname = $("#jobtitle").val();
                
               // alert(expectJob);
                /*
                var jobname2 = "${position.name}";

                if(roleid == "") {
                    //alert($("#companyId").dropdown('get search')[0]);
                    if(!$("#roleid").dropdown('get search')[0] || $("#roleid").dropdown('get search')[0]=="" ) {
                        if(jobname2!=$("#roleid").dropdown('get text')[0]) {
                            alert("您必须输入职位名称");
                            return false;
                        } 
                        jobname = jobname2;
                        roleid = 0;
                        
                    } else {
                        jobname = $("#roleid").dropdown('get search')[0];
                        roleid   = 0;
                    }
                }
                */

                if($('#hunter').hasClass('active') && $('#recom').hasClass('active') ) {
                    platformid = 3;
                } else if($('#hunter').hasClass('active')) {
                    platformid = 2;
                } else if($('#recom').hasClass('active')) {
                    platformid = 1;
                } else {
                    platformid = 0;
                }



                if(!location || location=="") {
                    alert("请填写负责区域");
                    return false;
                }
                if(location.length > 40) {
                    alert("负责区域名字太长");
                    return false;
                }
                if(!employeeNum || employeeNum =="") {
                    alert("请填写输入下属人数");
                    return false;
                }
                if(!/^[0-9]+$/.test(employeeNum) || employeeNum < 0 || employeeNum > 50000) {
                    alert("您填写的下属人数不合法，请填写大于0的正整数");
                    return false;
                }
                if(!majorDuty || majorDuty =="") {
                    alert("请填写关键要求");
                    return false;
                }


                if(!description || description =="") {
                    alert("请填写主要职责");
                    return false;
                }


                /* if(!reward || reward =="") {
                    alert("您必须输入推荐奖金");
                    return false;
                } */
                if(!/^[0-9]+$/.test(reward) || reward < 0 || reward > 100000) {
                    alert("推荐奖金区间在0~100000元");
                    return false;
                }
                if(!jobname || jobname == "") {
                    alert("请填写对外显示的职位名称");
                    return false;
                }
                if(jobname.length > 40) {
                    alert("对外显示的职位名称不得超过40个字");
                    return false;
                }
                if(!cityid) {
                    alert("请选择城市");
                    return false;
                }
                if(!roleid) {
                    alert("请选择选择职位");
                    return false;
                }

                if(!expectCompany && expectCompany != '' && expectCompany != 0) {
                    alert("请选择期望的来源公司1");
                    return false;
                }
                if(!expectJobId) {
                    alert("请选择期望候选人目前职位");
                    return false;
                }
                if(!majordomain) {
                    alert("请选择期望候选人所在细分领域");
                    return false;
                }
                var time = new Date($("#expiredDateStr").val());
                if (time.getTime()< new Date().getTime()) {
                    alert("职位有效期应设置在当天之后！")
                    return false;
                }
                if(expectCompany == expectCompany2 || expectCompany == expectCompany3) {
                    alert("所选期望公司不能重复！")
                    return false;
                }
                if(!expectCompany2 && expectCompany2 == expectCompany3) {
                    alert("所选期望公司不能重复！")
                    return false;
                }

                if(!platformid) {
                    alert("您必须选择发布平台");
                    return false;
                }
                if (platformid > 1 && reward <= 0){
                    alert("为了更好的在猎头平台进行职位推广，请设置大于0的推荐奖金");
                    return false;
                }

                $(this).addClass('disabled');
                var that = this;

                var param = {};
                var postUrl = '${ctx}/wechat/hunter/hr/creatJob.doechat/hunter/hr/creatJob.do';
                if (positionid && positionid == "0") {
                     postUrl = '${ctx}/wechat/hunter/hr/creatJob.do';
                }
                else
                {
                    postUrl = '${ctx}/wechat/hunter/hr/updateJob.do';
                    param['id']  = positionid;
                }
                var tag="";
                keywords.each(function () {
                    tag= tag+ "," + $(this).val();
                });
                tag = tag.substring(1);
                param['keywords'] = tag;
                param['name'] = jobname;
                param['roleid']   = roleid;
                param['cityId']   = cityid;
                param['location'] = location;
                param['employeeNum'] = employeeNum;
                param['annualPayment'] = annualPayment;
                param['majorDuty']   = majorDuty;
                param['reward'] = reward;
                param['expectcompanyid']  = expectCompany;
                param['expectcompanyid2'] = expectCompany2;
                param['expectcompanyid3'] = expectCompany3;
                param['expectJobId']      = expectJobId;
                param['expectJobName']      = expectJobName;
                param['expiredDateStr']   = expiredDateStr;
                param['platformID']       = platformid;
                param['comment']          = comment;
                param['domainID']         = domainid;
                param['description']  = description;
                var arrayLength = majordomain.length;
                if (arrayLength>0){
                  param['majorDomainId1'] = majordomain[0];
                  }
                if (arrayLength>1)
                {
                param['majorDomainId2'] = majordomain[1];
                }if (arrayLength>2)
                  {
                  param['majorDomainId3'] = majordomain[2];
                  }
		  
                $.ajax({
                    url: postUrl,
                    type: "POST",
                    data: param,
                    success: function(data, status){
                        if (data.errorcode == 0){
                            <%--var job = data.data;--%>
                            <%--var forwardUrl = '${ctx}/wechat/hunter/hr/toViewJob.page?positionid=' + job.id;--%>
                            <%--window.location.href = forwardUrl;--%>
                            alert("职位发布成功，请到职位列表中，查看更多职位详情！");
                            wx.closeWindow();
                        }else{
                            alert(data.msg);
                            $(that).removeClass('disabled');
                        }
                    },
                    error: function(){
                        alert("服务出错，请稍后尝试");
                        $(that).removeClass('disabled');
                    }
                });
            }
        } 
    };

    $("#expiredDateStr").blur(function () {
        var time = new Date($("#expiredDateStr").val());
        if (time.getTime()< new Date().getTime()) {
            alert("职位有效期应设置在当天之后！")
        }
    })

    var jobtitledata = [];
    
    <c:forEach items="${roles}" var="role">
        var tmp${role.id} = {};
        tmp${role.id}.title = "${role.name}";
        jobtitledata.push(tmp${role.id});
    </c:forEach>

    $(document).ready(function() {

        var companyIndex = 0;
        var company2Index = 0;
        var company3Index = 0;

        $('#expectcompanyid').change(function(){
            expectCompany = $('#expectcompanyid').val();
        })
        $('#expectcompanyid2').change(function(){
            expectCompany2 = $('#expectcompanyid2').val();
        })
        $('#expectcompanyid3').change(function(){
            expectCompany3 = $('#expectcompanyid3').val();
        })

        $('#majordomain').change(function(){
            var majordomain = $('#majordomain').val();
            if(majordomain == null){
                return;
            }
            var arrayLength = majordomain.length;
            var params = {};
            if (arrayLength>0){
                params['majorDomainId1'] = parseInt(majordomain[0]);
            }
            if (arrayLength>1)
            {
                params['majorDomainId2'] = parseInt(majordomain[1]);
            }if (arrayLength>2)
            {
                params['majorDomainId3'] = parseInt(majordomain[2]);
            }
            $.ajax({
                url: '${ctx}/wechat/hunter/hr/getCompanyByDomain.do',
                data: params,
                traditional: true,
                type: 'POST',
                success: function (data) {
                    if(data.errorcode == "1"){
                        var result = data.data
                        var com1 = document.getElementById('expectcompanyid');
                        var com2 = document.getElementById('expectcompanyid2');
                        var com3 = document.getElementById('expectcompanyid3');

                        com1.options.length = 0;
                        com2.options.length = 0;
                        com3.options.length = 0;
                        com1.options.add(new Option("请选择期望公司名称", 0));
                        com2.options.add(new Option("请选择期望公司名称", 0));
                        com3.options.add(new Option("请选择期望公司名称", 0));
                        for(var i=0;i<result.length;i++){
                            if(result[i].id == expectCompany){
                                companyIndex = i;
                            }
                            if(result[i].id == expectCompany2){
                                company2Index = i;
                            }
                            if(result[i].id == expectCompany3){
                                company3Index = i;
                            }
                            com1.options.add(new Option(result[i].name,result[i].id));
                            com2.options.add(new Option(result[i].name,result[i].id));
                            com3.options.add(new Option(result[i].name,result[i].id));
                        }
                        document.getElementById('expectcompanyid').selectedIndex = companyIndex;
                        document.getElementById('expectcompanyid2').selectedIndex = company2Index;
                        document.getElementById('expectcompanyid3').selectedIndex = company3Index;
                    }

                }
            })
        });
        $('#majordomain').dropdown('set value', dataarray);
        $('#keywords').dropdown('set value', keywordList);

          //allowAdditions: true,
        $('.dropdown').dropdown({
            maxSelections: 3,
            fullTextSearch:true
        });
        $('.dropdown1').dropdown({
            maxSelections: 2,
            fullTextSearch:true
        });
        $('.ui.search').search({
            source: jobtitledata,
            searchFullText: true
        });

        $('.button').on('click', handler.activate);
        wx.ready(function() {
            wx.hideOptionMenu();
        });
    });

</script>

            </form>
        </div>
    </div>
</div>
</body>
</html>